#include<iostream>   
#include <string>
using namespace std;  
struct TreeNode  
{  
    TreeNode* left;  
    TreeNode* right;  
    int data;  
};  
void PreOrder( TreeNode* BT )
{
	TreeNode* st[50],p;
	int top=-1,n=1;
	if(BT!=NULL){
		top++;
		st[top]=BT;
		while(top>-1){
		   BT=st[top];
		   top--;
		   	cout<<' '<<BT->data;
		if(BT->right!=NULL){
			top++;
			st[top]=BT->right;
		}
		if(BT->left!=NULL){
			top++;
			st[top]=BT->left;
		}
		}
	}

}
/*void PreOrder( TreeNode* BT )
{
	if(BT!=NULL)
	{
		printf(" %c",BT->data);
		PreOrder(BT->left);
		PreOrder(BT->right);
	}

}*/
TreeNode* findTree(int *post,int *in,int n)  
{   
    TreeNode* b=new TreeNode;
    int r,*p;  
    int k; 
	if(n<=0)  
        return NULL;  
    r=*(post+n-1);
    b->data=r;
    for(p=in;p<in+n;p++)  
    {  
        if(*p==r)  
        break;  
    }
    k=p-in;
    b->left=findTree(post,in,k);  
    b->right=findTree(post+k,p+1,n-k-1);  
    return b;
}  


int main()  
{  
    int in[33],post[33];  
    int N;  
    cin>>N;  
    int c;  
    for(int i=0;i<N;i++)  
    {  
        cin>>c;  
        post[i]=c;  
    }  
    for(int i=0;i<N;i++)  
    {  
        cin>>c;  
        in[i]=c;  
    }  
    TreeNode* Node=new TreeNode;  
    
    Node=findTree(post,in,N);
    printf("Preorder:") ;
    PreOrder(Node);
     return 0;
} 
